use bevy::{
    color::Color,
    ecs::component::Component,
    math::{IVec2, Vec3},
    time::Timer,
};
use bincode::{Decode, Encode};

use crate::config::goods_config::{FurnitureItem, GoodsItem, SeedItem};

/// 地图块位置
#[derive(Component, Encode, Decode, Default, PartialEq, Debug, Clone)]
pub struct BlockMapPos {
    pub x: i32,
    pub y: i32,
}

impl BlockMapPos {
    pub fn new(pos: IVec2) -> Self {
        Self { x: pos.x, y: pos.y }
    }
    pub fn get_world_pos(&self, z: f32) -> Vec3 {
        Vec3::new(
            (self.x as f32) * 100. - 50. as f32,
            (self.y as f32) * 100. - 50. as f32,
            z,
        )
    }
}

impl From<IVec2> for BlockMapPos {
    fn from(value: IVec2) -> Self {
        Self {
            x: value.x,
            y: value.y,
        }
    }
}

impl From<BlockMapPos> for IVec2 {
    fn from(value: BlockMapPos) -> Self {
        IVec2::new(value.x, value.y)
    }
}

///种植组件
#[derive(Component, Default, Encode, Decode, PartialEq, Debug, Clone)]
pub struct PlantingComponent {
    pub pos: BlockMapPos,
}

///作物组件
#[derive(Component, Default, Encode, Decode, PartialEq, Debug, Clone)]
pub struct CropComponent {
    pub day: u32,
    pub curr_stages: u32,
}

///作物配置组件
#[derive(Component, Default, Encode, Decode, PartialEq, Debug, Clone)]
pub struct CropConfigComponent(pub SeedItem);

///成熟作物
#[derive(Component)]
pub struct MatureCrop;

///快捷栏文本信息
#[derive(Component, Default)]
pub struct FastListInfoText;

///农场时间文本信息
#[derive(Component, Default)]
pub struct PlantingDayInfoText;

///背包信息
#[derive(Component)]
pub struct BagInfoText;

///选择地图块组件
#[derive(Component)]
pub struct SelectMapBlockComponent {
    pub can: bool,
}

#[derive(Component)]
pub struct SelectMapBlockText;

impl SelectMapBlockComponent {
    pub fn can(&mut self, can: bool) {
        self.can = can;
    }

    pub fn new() -> Self {
        Self { can: false }
    }

    pub fn get_colr(&self) -> Color {
        if self.can {
            Color::srgba(0.0, 1.0, 0.0, 1.0)
        } else {
            Color::srgba(1.0, 0.0, 0.0, 1.0)
        }
    }
}

///水
#[derive(Component)]
pub struct WaterComponent;

///田地
#[derive(Component)]
pub struct FieldComponent;

///草地
#[derive(Component)]
pub struct GrassComponent;

///家具
#[derive(Encode, Decode, Component, Clone, Debug, PartialEq)]
pub struct FurnitureComponent {
    pub config: FurnitureItem,
}

///农场时间计时器
#[derive(Component)]
pub struct PlantingTimer(pub Timer);

///物品组件
#[derive(Component, Clone, Debug)]
pub struct GoodsComponent {
    pub goods_item: GoodsItem,
    pub count: usize,
}

///物品被选中 TODO 先放在这里 之后在来实现
#[derive(Component)]
pub struct _GoodsSelectComponent;

///背包索引组件
#[derive(Component)]
pub struct BackpackIndexComponent {
    pub index: usize,
}

///快捷栏索引组件
#[derive(Component)]
pub struct FastListIndexComponent {
    pub index: usize,
}

///背包物品移动到快捷栏组件
#[derive(Component)]
pub struct BackpackMoveFastListComponent;

///快捷栏物品移动到背包组件
#[derive(Component)]
pub struct FastListMoveBackpackComponent;

///快捷栏按钮
#[derive(Component)]
pub struct FastListButton {
    pub index: usize,
}
